package com.qinglei.recoup.longconn.entity;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 用户数据数据体
 * 对应协议版本-01
 * 协议文档版本v1.4
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class UserDataBodyV1 extends BodyT {
    public static final int DATA_LENGTH = 138;

    /**
     * 帧数位 uint32_t
     */
    private long frameNo;
    /**
     * 呼吸相位数据 uint16_t * 10
     */
    private int[] breathPhaseData;
    /**
     * 心跳相位数据 uint16_t * 10
     */
    private int[] heartPhaseData;
    /**
     * 呼吸频率 次/分钟 uint8_t
     */
    private int breathBpm;
    /**
     * 心跳频率 次/分钟 uint8_t
     */
    private int heartBpm;
    /**
     * s1状态 uint8_t
     */
    private int s1;
    /**
     * s2状态 uint8_t
     */
    private int s2;
    /**
     * 解缠绕相位数据 uint32_t * 10
     */
    private long[] unwrappingPhaseData;
    /**
     * 一维距离像 uint8_t * 36
     */
    private int[] oneDimRangeProfile;
    /**
     * 呼吸深度 uint8_t
     */
    private int breathDepth;
    /**
     * 距离维功率强点索引 uint8_t
     */
    private int rangeDimPowerIndex;
    /**
     * 距离维功率峰值 uint8_t
     */
    private int rangeDimPowerPeak;
    /**
     * 噪声功率 uint8_t
     */
    private int noisePower;
    /**
     * 距离维谱峰宽度中值滤波结果 uint8_t
     */
    private int rangeDimSPWMFiltering;
    /**
     * 目标距离门 uint8_t
     */
    private int targetRangeGate;
    /**
     * 离床及其他异常状态 uint16_t
     */
    private int abnormal;
    /**
     * 离床时间（s）uint16_t
     */
    private int outBedTime;
    /**
     * 预留 uint8_t * 4
     */
    private int[] reserve;

    @Override
    public byte[] encode() {
        ByteBuf byteBuf = Unpooled.buffer(DATA_LENGTH);

        byteBuf.writeInt((int) this.getFrameNo());

        int[] breathPhaseData = this.getBreathPhaseData();
        for (int i = 0; i < 10; i++) {
            byteBuf.writeShort(breathPhaseData[i]);
        }

        int[] heartPhaseData = this.getHeartPhaseData();
        for (int i = 0; i < 10; i++) {
            byteBuf.writeShort(heartPhaseData[i]);
        }
        byteBuf.writeByte(this.getBreathBpm());
        byteBuf.writeByte(this.getHeartBpm());
        byteBuf.writeByte(this.getS1());
        byteBuf.writeByte(this.getS2());

        long[] unwrappingPhaseData = this.getUnwrappingPhaseData();
        for (int i = 0; i < 10; i++) {
            byteBuf.writeInt((int)unwrappingPhaseData[i]);
        }

        int[] oneDimRangeProfile = this.getOneDimRangeProfile();
        for (int i = 0; i < 36; i++) {
            byteBuf.writeByte(oneDimRangeProfile[i]);
        }

        byteBuf.writeByte(this.getBreathDepth());
        byteBuf.writeByte(this.getRangeDimPowerIndex());
        byteBuf.writeByte(this.getRangeDimPowerPeak());
        byteBuf.writeByte(this.getNoisePower());
        byteBuf.writeByte(this.getRangeDimSPWMFiltering());
        byteBuf.writeByte(this.getTargetRangeGate());
        byteBuf.writeShort(this.getAbnormal());
        byteBuf.writeShort(this.getOutBedTime());

        int[] reserve = this.getReserve();
        for (int i = 0; i < 4; i++) {
            byteBuf.writeByte(reserve[i]);
        }

        return byteBuf.array();
    }

    @Override
    public void decode(byte[] body) {
        ByteBuf byteBuf = Unpooled.wrappedBuffer(body);

        this.setFrameNo(byteBuf.readUnsignedInt());

        int[] breathPhaseData = new int[10];
        for (int i = 0; i < 10; i++) {
            breathPhaseData[i] = byteBuf.readUnsignedShort();
        }
        this.setBreathPhaseData(breathPhaseData);

        int[] heartPhaseData = new int[10];
        for (int i = 0; i < 10; i++) {
            heartPhaseData[i] = byteBuf.readUnsignedShort();
        }
        this.setHeartPhaseData(heartPhaseData);

        this.setBreathBpm(byteBuf.readUnsignedByte());
        this.setHeartBpm(byteBuf.readUnsignedByte());
        this.setS1(byteBuf.readUnsignedByte());
        this.setS2(byteBuf.readUnsignedByte());

        long[] unwrappingPhaseData = new long[10];
        for (int i = 0; i < 10; i++) {
            unwrappingPhaseData[i] = byteBuf.readUnsignedInt();
        }
        this.setUnwrappingPhaseData(unwrappingPhaseData);

        int[] oneDimRangeProfile = new int[36];
        for (int i = 0; i < 36; i++) {
            oneDimRangeProfile[i] = byteBuf.readUnsignedByte();
        }
        this.setOneDimRangeProfile(oneDimRangeProfile);

        this.setBreathDepth(byteBuf.readUnsignedByte());
        this.setRangeDimPowerIndex(byteBuf.readUnsignedByte());
        this.setRangeDimPowerPeak(byteBuf.readUnsignedByte());
        this.setNoisePower(byteBuf.readUnsignedByte());
        this.setRangeDimSPWMFiltering(byteBuf.readUnsignedByte());
        this.setTargetRangeGate(byteBuf.readUnsignedByte());
        this.setAbnormal(byteBuf.readUnsignedShort());
        this.setOutBedTime(byteBuf.readUnsignedShort());

        int[] reserve = new int[4];
        for (int i = 0; i < 4; i++) {
            reserve[i] = byteBuf.readUnsignedByte();
        }
        this.setReserve(reserve);
    }
}
